﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace ProtoGame.GameLib.Entities
{
    public abstract class AnimatedSprite : GraphicEntity
    {
        Tileset spriteGfx;
        Animator animations;
        Rectangle sourceRect;

        protected byte AnimationState { get; set; }
        
        public Vector2 Velocity { get; set; }

        public override Microsoft.Xna.Framework.Rectangle Bounds
        {
            get
            {
                return new Rectangle((int)Position.X, (int)Position.Y, (int)(spriteGfx.TilesWidth * Scale.X), (int)(spriteGfx.TilesHeight * Scale.Y));
            }
        }

        public AnimatedSprite(Animator animations, Tileset gfx)
        {
            this.spriteGfx = gfx;
            this.animations = animations;

            AnimationState = 0;
            Velocity = Vector2.Zero;
        }

        bool _movementUndone = false;
        protected void UndoMovement()
        {
            if (!_movementUndone)
            {
                Position -= Velocity;
                _movementUndone = true;
            }
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            // Repossibilita desfazer o movimento por causa da colisão com sólido
            _movementUndone = false;

            Position += Velocity;

            spriteGfx.getTileRectangle(animations.getAnimationFrame(AnimationState), out sourceRect);

            base.Update(gameTime);
        }

        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(spriteGfx.Texture, Position, sourceRect, ColorEffects, Rotation, Origin, Scale, Flip, LayerDepth);
        }
    }
}
